package app.netant.finishbook.qishu;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class QishuNetDetail extends NetAntBase {

	private QishuEntity entity;
	
	public QishuNetDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(QishuEntity entity){
		this.entity = entity;
	}
	
	public void analyseTags(){
		
		Elements activePart = document.select("#mainBody .pageMainArea #downInfoArea dd.downInfoRowL");
		
		String html = activePart.html();
		String[] params = html.split("<br>");
		
		for(int i=0;i<(params.length-1);i++){
			
			String simpleStr = NetAntBase.cutString(params[i],'<','>');
			simpleStr = simpleStr.replaceAll("\n", "").replaceAll("\r", "").trim();
			
			int point = simpleStr.indexOf("：");
			String tagName = "";
			String tagValue = "";
			if(point > 0){
				tagName = simpleStr.substring(0,point).trim();
			}
			if(point < simpleStr.length()-1){
				tagValue = simpleStr.substring(point+1).trim();
			}
			
			this.entity.setTag(tagName, tagValue);
		
		}
		
	}
	
	public void analyseHref(){
		
		Elements activePart = document.select("#mainBody .pageMainArea .mainstory .story #downAddress a");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			String fileFullName = href.substring(href.lastIndexOf("/")+1);
			String fileName = fileFullName.substring(0,fileFullName.lastIndexOf("."));
			this.entity.setDownPath(href);
			
			String extName = this.entity.getNovelName();
			if(extName == null || "".equals(extName)){
				this.entity.setNovelName(fileName);
				this.entity.setNovelFullName(fileFullName);
			}else{
				
				String ext = fileFullName.substring(fileFullName.lastIndexOf("."));
				this.entity.setNovelFullName(extName + ext);
				
			}
			
			
		}
		
	}
	
	public void analyseMemo(){
		
		Elements activePart = document.select("#mainBody .pageMainArea .mainstory .story #mainSoftIntro p");
		String memo = "";
		if(activePart.size()>1){
			memo = activePart.get(1).text();			
		}else if(activePart.size()>0){
			memo = activePart.get(0).text();
		}
		
		this.entity.setNovelMemo(memo);
		
	}
	
	public void analyseTitle(){
		
		Elements activePart = document.select("#mainBody .pageMainArea #downInfoArea #downInfoTitle");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			int index = title.indexOf("TXT");
			if(index > 0){
				title  = title.substring(0,index);
			}
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title);
		}
		
	}
	
	/**
	 * 信息分析
	 */
	public void analyse(){
		
		this.analyseTitle();
		this.analyseTags();		
		this.analyseHref();	
		this.analyseMemo();
		
		this.entity.download();
		this.entity.insert();
	}
	
	public static void main(String[] args) {
		
		QishuNetDetail detail = new QishuNetDetail("http://www.qishu.cc/txt/66804.html");
		
		detail.analyseTags();
		
		detail.analyseHref();
		
		detail.analyseMemo();
		
	}

}
